
package com.login.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import com.google.appengine.api.datastore.Entity;
import com.login.utils.EntityUtil;

/**
 * @author sang
 */
public class UserEntity extends BaseEntityImpl {

	private static final long serialVersionUID = 9L;
	
	/**
	 * Titles are stored in string list. Content language stored in first two 
	 * chars. 
	 */
	private String username;
	private String password;
	private String firstname;
	private String lastname;
	private Date publishDate;
	private Date endPublishDate;
	private Integer version;

	// not persisted
	private Map<String, String> titles;
	
	public UserEntity() {
		publishDate = new Date();
		version = 1;
		username = "";
		password = "";
		firstname = "";
		lastname = "";
		publishDate = new Date();
		endPublishDate = new Date();
	}
	
	@Override
	public void load(Entity entity) {
		super.load(entity);
		publishDate = EntityUtil.getDateProperty(entity, "publishDate");
		endPublishDate = EntityUtil.getDateProperty(entity, "endPublishDate");
		version = EntityUtil.getIntegerProperty(entity, "version", 1);
		username = EntityUtil.getStringProperty(entity, "username", "");
		password = EntityUtil.getStringProperty(entity, "password", "");
		firstname = EntityUtil.getStringProperty(entity, "firstname", "");
		lastname = EntityUtil.getStringProperty(entity, "lastname", "");
	}
	
	@Override
	public void save(Entity entity) {
		super.save(entity);
		EntityUtil.setProperty(entity, "username", username, true);
		EntityUtil.setProperty(entity, "password", password, false);
		EntityUtil.setProperty(entity, "firstname", firstname, false);
		EntityUtil.setProperty(entity, "lastname", lastname, false);
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public Date getPublishDate() {
		return publishDate;
	}

	public void setPublishDate(Date publishDate) {
		this.publishDate = publishDate;
	}

	public Date getEndPublishDate() {
		return endPublishDate;
	}

	public void setEndPublishDate(Date endPublishDate) {
		this.endPublishDate = endPublishDate;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public Map<String, String> getTitles() {
		return titles;
	}

	public void setTitles(Map<String, String> titles) {
		this.titles = titles;
	}
	
}
